package com.lancoo.znbkxx.teacher.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lancoo.znbkxx.teacher.R;


public class LogOutDialog extends Dialog {


    public LogOutDialog(Context context) {
        super(context);
    }

    public LogOutDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected LogOutDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private String message;//提示消息
        private String sbtnCancleText;
        private String sbtnEnsureText;
        private DialogInterface.OnClickListener sbtnCancleListener;
        private DialogInterface.OnClickListener sbtnEnsureListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            if (message == null) {
                this.message = "您没有填写提示信息哦";
            }
            this.message = message;
            return this;
        }

        public Builder setCancleButton(String sbtnCancleText, DialogInterface.OnClickListener sbtnCancleListener) {
            if (sbtnCancleText == null) {
                this.sbtnCancleText = "取消";
            }
            this.sbtnCancleText = sbtnCancleText;
            this.sbtnCancleListener = sbtnCancleListener;

            return this;
        }

        public Builder setEnsureButton(String sbtnEnsureText, DialogInterface.OnClickListener sbtnEnsureListener) {
            if (sbtnEnsureText == null) {
                this.sbtnEnsureText = "确定";
            }
            this.sbtnEnsureText = sbtnEnsureText;
            this.sbtnEnsureListener = sbtnEnsureListener;

            return this;
        }

        private TextView mTvMessage;//提示信息
        private ScaleButton mSBtnCancle;//退出
        private ScaleButton mSBtnEnsure;//继续


        public LogOutDialog create() {
            final LogOutDialog dialog = new LogOutDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_logout, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            mTvMessage =  view.findViewById(R.id.tv_dialogMessage);
            mSBtnCancle =  view.findViewById(R.id.sbtn_cancle);
            mSBtnEnsure =  view.findViewById(R.id.sbtn_ensure);
            mTvMessage.setText(message);
            mSBtnCancle.setText(sbtnCancleText);
            mSBtnEnsure.setText(sbtnEnsureText);
            Window window = dialog.getWindow();
            window.setWindowAnimations(R.style.MyDialogScale);//设置动画效果
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            mSBtnCancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sbtnCancleListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
                }
            });
            mSBtnEnsure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sbtnEnsureListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
                }
            });
            dialog.setCanceledOnTouchOutside(false);//禁止点击dialog外部销毁弹窗
            dialog.setOnKeyListener(new OnKeyListener() {//禁止系统返回键值响应
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    } else {
                        return false;
                    }
                }
            });
            return dialog;
        }
    }
}